package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.*;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     * @return
     */
    @Override
    @Transactional
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {

        //1.根据条件查询当前商品是否在购物车中
        ShoppingCart shoppingCart = new ShoppingCart();
        //复制DTO给entity
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //拿到userid并set
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //调用mapper查询条件购物车集合
        shoppingCart = shoppingCartMapper.queryShoppingCar(shoppingCart);

        //2.如果已经存在,则更新购买的数量
        if(shoppingCart != null){
            //如果存在,则数量+1
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            //修改商品数量
            shoppingCartMapper.updateNumberById(shoppingCart);
        }else {
            //如果不存在则创建一个新的对象
            shoppingCart = new ShoppingCart();
         //3.1判断当前添加的是菜品还是套餐
            //拿到DTO的菜品id,判断是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if(dishId != null){ //如果是菜品
                //根据菜品id查询菜品
                Dish dish = dishMapper.getById(dishId);
                //根据dto设置菜品口味
                shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                //设置菜品id
                shoppingCart.setDishId(dishId);
                //设置菜品名称
                shoppingCart.setName(dish.getName());
                //设置菜品图片
                shoppingCart.setImage(dish.getImage());
                //设置菜品价格
                shoppingCart.setAmount(dish.getPrice());
            }else{ //如果是套餐
                //根据套餐id查询套餐
                Setmeal setmeal = setmealMapper.geyById(shoppingCartDTO.getSetmealId());
                //设置套餐id
                setmeal.setId(shoppingCartDTO.getSetmealId());
                //设置套餐名称
                setmeal.setName(setmeal.getName());
                //设置套餐图片
                setmeal.setImage(setmeal.getImage());
                //设置套餐价格
                setmeal.setPrice(setmeal.getPrice());
            }

        //3.如果不存在,则插入数据,数量为1(每次+1)
            shoppingCart.setNumber(1); //设置数量为1
            shoppingCart.setCreateTime(LocalDateTime.now()); //创建时间
            shoppingCart.setUserId(BaseContext.getCurrentId()); //创建人的ID
        shoppingCartMapper.insert(shoppingCart); //把插入数据,存入数据库中
        }
    }

    /**
     * 查询购物车,并显示
     *
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCar() {
        //根据用户id查询购物车集合
        List<ShoppingCart> showShoppingCar = shoppingCartMapper.showShoppingCar(BaseContext.getCurrentId());
        return showShoppingCar;
    }

    /**
     * 清理购物车
     *
     * @return
     */
    @Override
    public Result clean() {
        //调用mapper层
        shoppingCartMapper.clean(BaseContext.getCurrentId());
        return Result.success();
    }

    /**
     * 点击减号
     *
     * @return
     */
    @Override
    public Result sub(ShoppingCartDTO shoppingCartDTO) {

        //非空校验
        if (shoppingCartDTO == null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }

        ShoppingCart shoppingCart = new ShoppingCart();
        //复制数据到entity
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart newShoppingCart = shoppingCartMapper.queryShoppingCar(shoppingCart);
        //大于1减一
        if (newShoppingCart.getNumber() > 1){
            newShoppingCart.setNumber(newShoppingCart.getNumber()-1);
            shoppingCartMapper.sub(newShoppingCart);
        }else

        //等于1删除
        if (newShoppingCart.getNumber() == 1){
            shoppingCartMapper.delete(newShoppingCart);
        }else

        if (newShoppingCart.getNumber() < 1){
            return  Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        return Result.success();
    }


}
